<template>
  <div class="searchj-box">
    <div class="row">
      <div class="left">
        <el-icon><Search /></el-icon>
        <span class="txt">筛选搜索</span>
      </div>
      <div class="right">
        <el-button @click="cz">重置</el-button>
        <el-button type="primary" @click="queryUserList">查询搜索</el-button>
      </div>
    </div>
    <div class="row">
      <span>角色名称:</span>
      <el-input
        placeholder="请输入姓名、账号搜索"
        v-model="queryParams.keyword"
      ></el-input>
    </div>
  </div>
  <div class="add-area">
    <div class="add-left">
      <el-icon>
        <Tickets />
      </el-icon>
      <span>数据列表</span>
    </div>
    <el-button
      type="primary"
      @click="showDialog = true"
      v-permission="'addUser'"
      >添加</el-button
    >
  </div>
  <el-table
    :data="userList"
    border
    table-layout="auto"
    cell-class-name="align-c"
  >
    <el-table-column label="编号" prop="id" />
    <el-table-column label="角色名称" prop="name" />
    <el-table-column label="描述" prop="description" />
    <el-table-column label="用户数" prop="adminCount" />
    <el-table-column label="添加时间" prop="createTime">
      <template #default="{ row }">
        <span>{{ formateDate(row.createTime) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="是否启用" prop="loginTime">
      <template #default="{ row }">
        <el-switch
          v-model="row.status"
          :active-value="1"
          :inactive-value="0"
          @change="switchStatus(row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          @click="allocMenu(row.id)"
          v-permission="'allocRole'"
          >分配菜单</el-button
        >
        <el-button
          text
          type="primary"
          @click="router.push('/ums/allocResource?id=' + row.id)"
          v-permission="'allocRole'"
          >分配资源</el-button
        >
        <el-button
          text
          type="primary"
          @click="editUser(row.id)"
          v-permission="'editUser'"
          >编辑</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <ElConfigProvider :locale="zhCn">
    <div class="page">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="queryUserList"
        @current-change="queryUserList"
      />
    </div>
  </ElConfigProvider>
  <RoleCreate :show="showDialog" @hide="closeDialog" :id="editId" />
</template>
<script setup lang="ts">
import { Search, Tickets } from "@element-plus/icons-vue";
import { ref, reactive } from "vue";
import { roleListApi, switchRoleStatusApi, getUserById } from "./api";
import { ElConfigProvider, ElMessage } from "element-plus";
import RoleCreate from "./roleCreate.vue";
import zhCn from "element-plus/es/locale/lang/zh-cn";
import { formateDate } from "@/utils";
import router from "@/router";
const userList = ref<IRoleModel[]>([]);
const total = ref(0);
const editId = ref(0);
const showDialog = ref(false);

const allocMenu = (id: number) => {
  console.log(12111);

  router.push("/ums/allocMenu?id=" + id);
};

const editUser = (id: number) => {
  showDialog.value = true;
  editId.value = id;
  console.log(editId.value);
};

const closeDialog = (isReload?: boolean) => {
  showDialog.value = false;
  editId.value = 0;
  // isReload 表示是否需要刷新数据
  if (isReload) queryUserList();
};

const queryParams = reactive<IUserListParams>({
  keyword: "",
  pageNum: 1,
  pageSize: 10,
});

const queryUserList = () => {
  roleListApi(queryParams).then((res) => {
    if (res.code === 200) {
      userList.value = res.data.list;
      total.value = res.data.total;
    }
    console.log(userList.value);
  });
};

const cz = () => {
  queryParams.keyword = "";
};

const switchStatus = (row: IRoleModel) => {
  switchRoleStatusApi(row.id!, row.status)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("修改成功");
      } else {
        row.status = row.status === 1 ? 0 : 1;
      }
    })
    .catch(() => {
      row.status = row.status === 1 ? 0 : 1;
    });
};
queryUserList();
</script>
<style lang="less" scoped>
:deep(.align-c) {
  .cell {
    text-align: center !important;
  }
}
.searchj-box {
  box-shadow: 0 0 4px 2px #ccc;
  border-radius: 10px;
  padding: 0 20px 10px;
  margin-bottom: 20px;

  .row {
    .txt {
      margin-left: 6px;
      position: relative;
      bottom: 2px;
    }
    display: flex;
    padding: 10px 0 10px 0;
    align-items: center;
    &:first-child {
      justify-content: space-between;
    }
    &:last-child {
      width: 400px;
      span {
        width: 130px;
      }
    }
  }
}
.page {
  padding: 20px;
  display: flex;
  justify-content: flex-end;
}
.add-area {
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  margin-bottom: 20px;
  border-radius: 4px;
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  transition: 0.3s;
  .add-left {
    display: flex;
    align-items: flex-end;
    span {
      padding-left: 10px;
      font-size: 14px;
    }
  }
}
</style>
